import request from '../../http/request'

export default {
    async getRolemenubyjyrole(_this: any) {//获取用户的系统菜单信息
        const menu = {
            xtid: import.meta.env.VITE_APP_XTID,//传入xtid查询系统的菜单信息
            cdId: '0'
        }
        let _that = this
        await request({
            url: '/menu/getRolemenubyjyrole',
            method: 'post',
            params: menu
        }).then((res: { code: string; data: string | any[] }) => {
            console.log('菜单', res)
            if (res.code === '0') {
                let list: never[] = []
                sessionStorage.setItem("wgfwmenuRouter", JSON.stringify(list));//路由地址
                //sessionStorage.setItem("menuList", JSON.stringify(list));//admin菜单
                //sessionStorage.setItem("menuList", JSON.stringify(res.data));//admin菜单
                _this.$store.commit('setwgfwMenuList', res.data)
                let menuRouter = []
                if (res.data.length > 0) {
                    menuRouter = _that.readNodes(res.data, '')
                    sessionStorage.setItem("wgfwmenuRouter", JSON.stringify(menuRouter));//路由地址
                }
            }
        })
    },
    async getDwxxtTree(_this: any, dwxx: any) {//获取当前登录用户的树形单位和全部的树形单位
        await request({
            url: '/dwxx/getDwTreebybm',
            method: 'get',
            params: dwxx,
        }).then((res: { code: string; data: string | any[] }) => {
            if (res.code !== '0') {
                return;
            }
            if (res.data.length < 1) {
                return;
            }
            _this.$store.commit('setdwTree', res.data[0].userDwtree)
        })
    },

    arrTotree(arr: string | any[], nodes: { id: any; label: any; cdcs: any; icon: any; children: never[] }[]) {//遍历赋值菜单树
        if (!arr.length) return []
        for (let item of arr) {
            let node = { id: item.id, label: item.label, cdcs: item.cdcs, icon: item.ico, children: [] }
            nodes.push(node)
            if (item.children && item.children.length) this.arrTotree(item.children, node.children)
        }
        return nodes
    },
    readNodes(nodes: string | any[], arr: any) {//遍历获取路由数组
        for (let item of nodes) {
            if (item.cdlydz !== null && item.cdlydz !== '' && JSON.parse(item.cdlydz).length > 0) {//判断路由是否为空 数组是否存在
                arr.push(JSON.parse(item.cdlydz))
            }
            if (item.children && item.children.length > 0) this.readNodes(item.children, arr)
        }
        return arr
    }
}